package com.crawler.search.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateParseUtil {
    static final List<String> regexList = new ArrayList<>();

    // By PA Aug 24, 2015
    static {
        regexList.add("yyyy");
        regexList.add("yyyy-MM-dd HH:mm:ss");
        regexList.add("yyyy-MM-dd HH:mm:ss.0");
        regexList.add("MM\\.dd");
        regexList.add("yyyy-MM-dd HH:mm");
        regexList.add("MM/dd/yyyy");
        regexList.add("yyyy-MM-dd");
        regexList.add("MMM dd, yyyy");
        regexList.add("于yyyy年MM月dd日");
        regexList.add("yyyy年MM月dd日");
        regexList.add("MMM dd, yyyy");
        regexList.add("MMM dd,HH:mm:ss");
        // Tue, 04/26/2016 - 07:23
        regexList.add("EEEE, MM/dd/yyyy - HH:mm");
        // 2016-04-14T12:52:17Z
        regexList.add("yyyy-MM-ddHH:mm:ss");
        regexList.add("yyyy-MM-ddHH:mm:ss");
    }


	public static String fullTimePattern = "yyyy-MM-dd HH:mm:ss";
	
	@SuppressWarnings("unused")
	private static String datePattern = "yyyy-MM-dd";
    
    public static String tryParse(String date) {
        return tryParse(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String tryParse(String date, String format) {

        SimpleDateFormat sdf2 = new SimpleDateFormat(format);

        for (int i = 0; i < regexList.size(); i++) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(regexList.get(i), Locale.ENGLISH);
                return sdf2.format(sdf.parse(date));
            } catch (Exception e) {
            }
        }
        return null;
    }
    
    public static Date tryParseDate(String date) {
    	Date dates=new Date();
    	for (int i = 0; i < regexList.size(); i++) {
    		try {
    			SimpleDateFormat sdf = new SimpleDateFormat(regexList.get(i), Locale.ENGLISH);
    			return sdf.parse(date);
    		} catch (Exception e) {
    		}
    	}
    	return dates;
    }

    public static Date getDate(String date) {

        for (int i = 0; i < regexList.size(); i++) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(regexList.get(i), Locale.ENGLISH);
                return sdf.parse(date);
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static Date getFixDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss", Locale.ENGLISH);
            return sdf.parse(date);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * @param date time  long
     * @return yyyy-MM-dd  HH:mm:ss
     */
    public static String getFixDateStr(Long date) {
        return getDateStr("yyyy-MM-dd HH:mm:ss", date);
    }

    public static String getFixDateStr(Date date) {
        return getDateStr("yyyy-MM-dd HH:mm:ss", date);
    }

    public static String getyyyy_MM_dd(Long date) {
        return getDateStr("yyyy-MM-dd", date);
    }

    public static String getyyyy_MM_dd(Date date) {
        return getDateStr("yyyy-MM-dd", date);
    }

    public static String getDateStr(String simpleDateFormatStr, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatStr, Locale.ENGLISH);
        try {
            return sdf.format(date);
        } catch (Exception e) {
        }
        return null;
    }

    public static String getDateStr(String simpleDateFormatStr, Long date) {
        SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatStr, Locale.ENGLISH);
        try {
            return sdf.format(new Date(date));
        } catch (Exception e) {
        }
        return null;
    }
    
    /**
     * 获取指定的时间
     * @param pattern 时间格式
     * @param i 偏移量
     * @param calType 单位
     * @return 结果 
     */
    public static String timeExpire(String pattern, int i, int calType) {
        Calendar now = Calendar.getInstance();
        now.add(calType, i);
        return DateFormatUtils.format(now.getTimeInMillis(), pattern);
    }
    
    /**
     * 获取指定的时间
     * @param date 时间格式
     * @return 结果
     */
    public static String getZeroDate(Date date) {
    	return getyyyy_MM_dd(date)+" 00:00:00";
    }
    
    
    /**
     * 获取指定的时间
     * @param date 时间格式
     * @return 结果
     */
    public static String getEndDate(Date date) {
    	return getyyyy_MM_dd(date)+" 23:59:59";
    }
    

    

}
